<template>

    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades">异常订单</i>
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">

                <el-date-picker v-model="query.deadLine" placeholder="交货日期" type="month" class="handle-search mr10" ></el-date-picker>
                <el-input v-model="query.company" placeholder="公司名称" class="handle-search mr10"></el-input>
                <el-select v-model="query.status" placeholder="订单状态" class="handle-select mr10">
                    <el-option key="1" label="异常中" :value="0"></el-option>
                    <el-option key="2" label="已恢复" :value="1"></el-option>
                </el-select>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <!--<el-button icon="el-icon-upload" @click="handleImport">导入</el-button>-->
            </div>
            <el-table
                    :data="tableData"
                    border
                    class="table"
                    id="out-table"
                    ref="multipleTable"
                    header-cell-class-name="table-header"
                    @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>

                <el-table-column type="index" width="55" align="center" label="序号"></el-table-column>
                <el-table-column prop="order.orderNum" align="center" label="订单号" width="150"></el-table-column>
                <el-table-column prop="order.company" align="center" label="公司" width="110"></el-table-column>
                <el-table-column prop="order.deadLine" align="center" label="交货日期" width="110" sortable></el-table-column>
                <el-table-column prop="reportTime" align="center" label="上报时间" width="110" sortable ></el-table-column>
                <el-table-column prop="status" align="center" label="状态" :formatter="statusFormat" width="100"  ></el-table-column>
                <el-table-column prop="content" align="center" label="描述" ></el-table-column>
                <el-table-column label="操作"  align="center" width="170">
                    <template slot-scope="scope">
                        <el-button
                                type="text"
                                icon="el-icon-edit"
                                @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                                type="text"
                                icon="el-icon-check"
                                class="green"
                                @click="recover(scope.row)"
                        >恢复</el-button>

                    </template>
                </el-table-column>

            </el-table>
            <div class="pagination">
                <el-pagination
                        background
                        layout="total, prev, pager, next"
                        :current-page="query.pageIndex"
                        :page-size="query.pageSize"
                        :total="pageTotal"
                        @current-change="handlePageChange"
                        align="center"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%" >
            <el-form ref="form" :model="form" :rules="rules" label-width="95px">

                <el-form-item label="订单号" prop="orderNum" >
                    <el-input v-model="form.order.orderNum" class="handle-input mr10" :disabled="true"></el-input>
                </el-form-item>


                <el-form-item label="客户" prop="company"  >
                    <el-input v-model="form.order.company" class="handle-input mr10" :disabled="true"></el-input>
                </el-form-item>

                <el-form-item label="描述" prop="content"  >
                    <el-input type="textarea" v-model="form.content" class="handle-input mr10"></el-input>
                </el-form-item>


            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>


    </div>
</template>

<script>
    export default {
        name: "orderException",
        data() {
            return {
                query: {
                    company:'',
                    pageIndex: 1,
                    pageSize: 10
                },
                productList:[],
                tableData: [],
                multipleSelection: [],
                form:{
                    order:""
                },
                editVisible: false,
                idx:-1,
                pageTotal: 0,
                rules:{
                    deadLine: [{ required: true, message: '请输入订单号', trigger: 'blur' }],
                }

            };

        },
        created() {

            this.handleSearch();

        },
        methods: {
            //恢复
            recover(row){
                console.log(row.id);
                let data = 'id='+row.id+'&orderId='+row.order.id;
                this.$axios.post('/api/orders/recoverException',data,{headers:{
                        'Content-Type':"application/x-www-form-urlencoded;charset=utf-8"
                    }}).then(res=>{

                    if(res.data.code==="200"){
                        this.$message.success(res.data.message);
                        this.handleSearch();

                    }else {
                        this.$message.error(res.data.message);
                    }

                })
            },

            handleSelectionChange(val) {
                this.multipleSelection = val;
            },

            // 编辑操作
            handleEdit(index, row) {

                this.idx = index;
                this.form = row;
                this.editVisible = true;
                console.log(this.form);
            },

            //tableData 翻译status
            statusFormat(row, column){

                if (row.status === 1) {
                    return '已恢复'
                } else  {
                    return '异常中'
                }
            },

            // 保存编辑
            saveEdit() {
                console.log(this.form);

                this.editVisible = false;
                this.$axios.post('/api/orders/editException',this.form).then(res=>{
                    if(res.data.code==="200"){
                        this.$message.success(res.data.message);

                    }else {
                        this.$message.error(res.data.message);
                    }

                }).catch(
                    error=>{
                        this.$message.error("修改失败");
                    }
                ).finally(()=>{
                    this.handleSearch();
                });

            },

            // 触发搜索按钮
            handleSearch() {
                this.$axios({
                    method:"post",
                    url:"/api/orders/searchException",
                    data:{

                        deadLine:this.query.deadLine,
                        company:this.query.company,
                        status:this.query.status
                    }
                }).then(res =>{
                    if (res.data.code!=='200') {
                        this.$message.error(res.data.message);
                    }else {
                        this.pageSplit(res.data.data);
                    }
                });

            },
            //分页
            pageSplit(data){
                this.pageTotal=data.length;
                let start= (this.query.pageIndex-1)*this.query.pageSize;
                let end = this.query.pageIndex*this.query.pageSize<this.pageTotal?this.query.pageIndex*this.query.pageSize:this.pageTotal;
                this.tableData=[];
                for (let i=start;i<end;i++){
                    this.tableData[i-start]=data[i];
                }
            },

            // 分页导航
            handlePageChange(val) {

                this.$set(this.query, 'pageIndex', val);
                this.getData();
            },


        }
    }
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-search{
        width: 130px;
        display: inline-block;
    }

    .handle-input {
        width: 200px;
        display: inline-block;
    }
    .table {
        width: 100%;
        font-size: 14px;
    }
    .green {
        color: #67C23A;
    }

    .mr10 {
        margin-right: 10px;
    }

</style>